package com.flycent.hqhs.ui;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.flycent.hqhs.R;

/**
 *  WebView显示
 * @author fbl
 */
public class WebViewActivity extends BaseActivity{
	@ViewInject(id = R.id.myWeb) WebView myWeb;
	@ViewInject(id = R.id.progress_bar) ProgressBar mypProgressBar;
	
	private String mWebUrl;

	@SuppressWarnings("deprecation")
	@SuppressLint("SetJavaScriptEnabled") 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_webview);
		FinalActivity.initInjectedView(this);
		
		mWebUrl = getIntent().getStringExtra("url");


		WebSettings webSettings = myWeb.getSettings();
		//设置支持javaScript
		webSettings.setJavaScriptEnabled(true); 
		//设置可以访问文件
		webSettings.setAllowFileAccess(true);
		//设置可以缩放
		webSettings.setSupportZoom(true);
		// 设置默认缩放方式尺寸是far
		webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
		// 设置出现缩放工具
		webSettings.setBuiltInZoomControls(true);
		webSettings.setDefaultFontSize(20);

		//url  
		myWeb.loadUrl(mWebUrl); 

		// 设置WebViewClient
		
		

		myWeb.setWebViewClient(new WebViewClient(){

			// url拦截
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器器加载页面
				view.loadUrl(url);
				// 相应完成返回true
				return true;
				// return super.shouldOverrideUrlLoading(view, url);
			}

			// 页面开始加载
			@Override
			public void onPageStarted(WebView view, String url,Bitmap favicon) {
				mypProgressBar.setVisibility(View.VISIBLE);
				super.onPageStarted(view, url, favicon);
			}

			//页面加载完成
			@Override
			public void onPageFinished(WebView view, String url) {
				mypProgressBar.setVisibility(View.GONE);
				super.onPageFinished(view, url);
			}

			// WebView加载的所有资源url
			@Override
			public void onLoadResource(WebView view, String url) {
				super.onLoadResource(view, url);
			}

			//
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
			}

		});

		// 设置WebChromeClient
		myWeb.setWebChromeClient(new WebChromeClient() {

			// 处理javascript中的alert
			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					JsResult result) {
				return super.onJsAlert(view, url, message, result);
			}

			// 处理javascript中的confirm
			@Override
			public boolean onJsConfirm(WebView view, String url,
					String message, JsResult result) {
				return super.onJsConfirm(view, url, message, result);
			}

			// 处理javascript中的prompt
			@Override
			public boolean onJsPrompt(WebView view, String url, String message,
					String defaultValue, JsPromptResult result) {
				return super.onJsPrompt(view, url, message, defaultValue, result);
			}

			//网页加载的进度条
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				mypProgressBar.setProgress(newProgress);
				super.onProgressChanged(view, newProgress);
			}

			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
			}
		});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {  
		if (keyCode == KeyEvent.KEYCODE_BACK ) {  
			if( myWeb.canGoBack()){
				myWeb.goBack();
				System.out.println("aaaaaaaaa");
			}else {
				System.out.println("bbbbbbbbb");
				finish();
			}
		}
		return false;
	}

}
